package com.dubious.interview.graph;

public class Edge<S extends Object> {
    private int vertexA;
    private int vertexB;
    private S weight;
    
    public Edge(int vertexA, int vertexB, S weight)
    {
        this.vertexA = vertexA;
        this.vertexB = vertexB;
        this.weight = weight;
    }
    
    public int getVertexA()
    {
        return vertexA;
    }
    
    public int getVertexB()
    {
        return vertexB;
    }
    
    public S getWeight()
    {
        return weight;
    }
    
    @Override
    public boolean equals(Object other)
    {
        if(other == null || !(other instanceof Edge))
        {
            return false;
        }
        
        Edge otherEdge = (Edge)other;
        
        // compare weights, first
        if(this.weight == null && otherEdge.weight != null)
        {
            return false;
        } else if(this.weight != null && !this.weight.equals(otherEdge.weight))
        {
            return false;
        }
        
        // compare teh rest
        return ((Edge)other).vertexA == this.vertexA &&
                ((Edge)other).vertexB == this.vertexB;
    }
    
    @Override
    public int hashCode()
    {
        return vertexA ^ vertexB ^ weight.hashCode();
    }
}
